package com.yssoft.service.inspection;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TCode;
import com.yssoft.entity.core.TTechnicIndex;
import com.yssoft.entity.inspection.TInspStandardEnt;
import com.yssoft.mapper.core.TCodeMapper;
import com.yssoft.mapper.inspection.TInspStandardDetailMapper;
import com.yssoft.mapper.inspection.TInspStandardEntMapper;
import com.yssoft.mapper.inspection.TInspStandardMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;

/**
 * @author 戈超
 * @time 2018/5/18 上午 09:10 @ 检验管理service
 */
@Service
public class InspStandardService {

	@Resource
	private TInspStandardMapper tInspStandardMapper;
	@Resource
	private TInspStandardDetailMapper tInspStandardDetailMapper;
	@Resource
	private TCodeMapper tCodeMapper;
	@Resource
	private TInspStandardEntMapper tInspStandardEntMapper;

	/**
	 * 查询产品检验列表
	 * 
	 * @param page
	 * @param limit
	 * @param sourceName
	 * @param pType
	 * @return
	 */
	public JSONObject queryInspStandardEntPage(int page, int limit, String sourceName, String pType) {
		Integer entId = UserHelper.getCurrentCompanyId();
		PageHelper.startPage(page, limit);// 下面第一个查询结果进行分页
		Map<String, Object> param = new HashMap<>();
		param.put("sourceName", sourceName);
		param.put("pType", pType);
		param.put("tEId", entId);
		param.put("deleteFlag", "0");
		List<Map<String, Object>> inspStandardList = tInspStandardMapper.queryAllSql(param);
		int count = tInspStandardMapper.queryCountSql(param);// 查询条数
		JSONObject jsonObject = LayuiUtil.getJSONObjectByMap(inspStandardList, count, "", "");
		return jsonObject;
	}

	/**
	 * 查询产品类型
	 * @return
	 */
	public List<TCode> queryCodeList() {
		TCode code = new TCode();
		code.setDeleteFlag("0");
		code.setClassify("material");
		List<TCode> codeList = tCodeMapper.select(code);
		return codeList;
	}

	/**
	 * 查询产品列表
	 * @param page
	 * @param limit
	 * @param sourceName
	 * @param pType
	 * @return
	 */
	public JSONObject querySourcePage(int page, int limit, String sourceName, String pType) {
		Integer entId = UserHelper.getCurrentCompanyId();
		PageHelper.startPage(page, limit);// 下面第一个查询结果进行分页
		Map<String, Object> param = new HashMap<>();
		param.put("deleteFlag", "0");
		if(pType == null){
			pType = "1";
		}
		param.put("pType", pType);
		param.put("sourceName", sourceName);
		param.put("tEId", entId);
		switch (pType) {
		case "3":
			param.put("classify", "rawClassify");
			break;
		case "4":
			param.put("classify", "packagingClassify");
			break;
		case "5":
			param.put("classify", "accessoriesClassify");
			break;
		case "6":
			param.put("classify", "additiveClassify");
			break;
		default:
			break;
		}
		List<Map<String, Object>> sourceList = tInspStandardMapper.querySourceAllSql(param);
		int count = tInspStandardMapper.querySourceCountSql(param);// 查询条数
		JSONObject jsonObject = LayuiUtil.getJSONObjectByMap(sourceList, count, "", "");
		return jsonObject;
	}

	/**
	 * 编辑保存企业产品检验标准信息
	 * @param sourceId
	 * @param type
	 * @param delIds 
	 * @param name
	 * @param min_value
	 * @param max_value
	 * @param unit
	 * @param insp_step
	 * @param source
	 * @param standardEntId 
	 */
	@Transactional(rollbackFor = Exception.class)
	public void saveInspect(Integer sourceId, String type, String delIds, String[] standardEntId, String[] name, String[] min_value, String[] max_value,
			String[] unit, String[] insp_step, String[] source) {
		/*删除去除工艺指标*/
		if(delIds != null && delIds.length() > 0){
			String[] delId = delIds.split(",");
			for(String dId : delId){
				TInspStandardEnt tInspStandardEnt = new TInspStandardEnt();
				tInspStandardEnt.setDeleteFlag("1");
				tInspStandardEnt.setId(Integer.parseInt(dId));
				tInspStandardEntMapper.updateByPrimaryKeySelective(tInspStandardEnt);
			}
		}
		boolean isSource = false;
		boolean isInspStep = false;
		if(source != null && source.length > 0){
			isSource = true;
		}
		if(insp_step != null && insp_step.length > 0){
			isInspStep = true;
		}
		if(standardEntId != null && standardEntId.length > 0){
			for(int i= 0; i < standardEntId.length; i++){
				TInspStandardEnt tInspStandardEnt = new TInspStandardEnt();
				tInspStandardEnt.setpType(type);
				if("1".equals(type)){
					tInspStandardEnt.settPId(sourceId);
				} else if("2".equals(type)){
					tInspStandardEnt.settSpId(sourceId);
				} else {
					tInspStandardEnt.setpType("3");
					tInspStandardEnt.settMId(sourceId);
				}
				tInspStandardEnt.setName(name[i]);
				tInspStandardEnt.setMinValue(min_value[i]);
				tInspStandardEnt.setMaxValue(max_value[i]);
				tInspStandardEnt.setUnit(unit[i]);
				if(isInspStep){
					tInspStandardEnt.setInspStep(insp_step[i]);
				}
				if(isSource){
					tInspStandardEnt.setSource(source[i]);
				}
				if("newId".equals(standardEntId[i])){
					tInspStandardEnt.setCreateTime(new Date());
					tInspStandardEnt.setDeleteFlag("0");
					tInspStandardEnt.setCreatorId(UserHelper.getCurrentUser().getPersonId());
					tInspStandardEnt.setCreatorName(UserHelper.getCurrentUser().getPersonName());
					tInspStandardEnt.settEId(UserHelper.getCurrentCompanyId());
					tInspStandardEntMapper.insertSelective(tInspStandardEnt);
				} else {
					tInspStandardEnt.setId(Integer.parseInt(standardEntId[i]));
					tInspStandardEntMapper.updateByPrimaryKeySelective(tInspStandardEnt);
				}
			}
		}
	}

	/**
	 * 根据产品ID查询企业产品检验信息
	 * @param inspectId
	 * @param pType
	 * @return
	 */
	public Map<String, Object> queryInspectEntById(Integer inspectId, String pType) {
		Map<String, Object> param = new HashMap<>();
		param.put("inspectId", inspectId);
		param.put("pType", pType);
		Map<String, Object> map = tInspStandardMapper.querySourceById(param);
		map.put("pType", pType);
		if(map != null){
			TInspStandardEnt tInspStandardEnt = new TInspStandardEnt();
			tInspStandardEnt.setDeleteFlag("0");
			tInspStandardEnt.setpType(pType);
			if("1".equals(pType)){
				tInspStandardEnt.settPId(inspectId);
			} else if("2".equals(pType)){
				tInspStandardEnt.settSpId(inspectId);
			} else {
				tInspStandardEnt.settMId(inspectId);
			}
			List<TInspStandardEnt> inspectList = tInspStandardEntMapper.select(tInspStandardEnt);
			map.put("inspectList", inspectList);
		}
		return map;
	}

	/**
	 * 删除企业产品检验标准信息
	 * @param string
	 * @param string2
	 */
	public void delInspect(String inspectId, String pType) {
		Map<String, Object> param = new HashMap<>();
		param.put("inspectId", inspectId);
		param.put("pType", pType);
		tInspStandardEntMapper.delInspect(param);
	}
}
